package com.example.conference.service;

import cn.dev33.satoken.stp.StpUtil;
import com.example.conference.SaToken.StpInterfaceImpl;
import com.example.conference.domain.User;
import com.example.conference.domain.dto.PageParam;
import com.example.conference.domain.vo.UserVo;
import com.example.conference.mapper.RoleMapper;
import com.example.conference.mapper.UserMapper;
import com.example.conference.utils.JwtUtil;
import com.example.conference.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    StpInterfaceImpl stpInterface;

    public Result queryUser(PageParam page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<UserVo> users = userMapper.queryToUserMap();
        return Result.success().data("items", users.doSelectPageInfo(()-> userMapper.queryToUserMap()));
    }
    public Result queryUserByName(PageParam page,String name){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<UserVo> users = userMapper.queryByName(name);
        return Result.success().data("items", users.doSelectPageInfo(()-> userMapper.queryByName(name)));
    }

    public Result deleteUser(Integer id) {
        if (userMapper.delete(id) != null) {
            return Result.success();
        }
        return Result.error("删除用户失败,用户Id不存在");
    }
    public Result queryUserByRole(PageParam page, Integer roleId){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<User> users = userMapper.queryByRole(roleId);
        return Result.success().data("items", users.doSelectPageInfo(()-> userMapper.queryByRole(roleId)));
    }

    public List<User> queryUserByRoleAndDept(Integer roleId, Integer deptId){return userMapper.queryUserByRoleAndDept(roleId,deptId);}

    public Result addUser(User u) {
        if (userMapper.add(u) != null) {
            return Result.success();
        }
        return Result.error("添加用户失败,用户Id可能已经存在");
    }
    public Result updateUser(User u) {
        if (userMapper.update(u) != null) {
            return Result.success();
        }
        return Result.error("修改用户失败,用户Id可能不存在");
    }

    public Result checklogin(User u) {
        if (userMapper.checklogin(u) != null) {
            StpUtil.login(u.getUserId());

            String token = StpUtil.getTokenValue();
            System.out.println("token : " + token);

            return Result.success().data("token",token);
        } else {
            return Result.error("账号或密码错误,请重试");
        }
    }

    public Result info() {
        Integer id = StpUtil.getLoginIdAsInt();
        System.out.println("id : " + id);
        UserVo user = userMapper.queryByIdToVo(id);
        String name = user.getUsername();
        String avatar = user.getUserAvatar();
        Integer majorId = user.getMajor().getMajorId();
        String tmp = user.getRole().getRoleName();
        String[] roles = {};
        switch (tmp) {
            case "管理员" -> roles = new String[]{"admin"};
            case "教师" -> roles = new String[]{"teacher"};
            case "学生" -> roles = new String[]{"student"};
        }
        return Result.success().data("name",name).data("avatar",avatar).data("userId",id).data("majorId",majorId)
                .data("roles",roles);
    }
}
